<?php
/**
 * This file is part of Hyperf Mall.
 *
 * @link     https://gitee.com/scwlkj/hypref_mall
 * contact  hyperf_mall@163.com
 */
namespace App\Repository;

use App\Constants\Message;
use App\Constants\PointCode;
use App\Exception\BaseException;
use App\Model\BaseModel;
use App\Model\SignModel;
use App\Utils\DB;
use App\Utils\Log;
use Carbon\Carbon;
use Hyperf\Database\Model\Model;
use Hyperf\Di\Annotation\Inject;
use Swoole\Exception;

class SignRepository extends BaseRepository
{
    /**
     * @Inject
     * @var SignModel
     */
    protected $model;

    /**
     * Notes: 积分签到
     * Author: Bruce.z
     * DateTime: 2022/7/18 17:34
     * @param $data
     * @return bool
     * @throws BaseException
     */
    public function baseAdd($data): bool
    {
        $data['point'] = 1;//签到奖励 当前固定1积分

        $time = Carbon::yesterday()->timestamp;
        $today_time = Carbon::today()->timestamp;
        $re = $this->getOne(['member_id'=>$data['member_id'], ['create_time','>',$today_time]],['days','id']);
        if(!empty($re)) throw new BaseException(BaseException::BASE_ERROR, '重复签到!');

        $re = $this->getOne(['member_id'=>$data['member_id'], ['create_time','>',$time]],['days','id']);
        $data['days'] = $re['days'] ?? 0;
        $data['days'] += 1;

        $re = MemberPointRepository::getInstance()->getOne(['member_id'=>$data['member_id']],['point']);
        DB::beginTransaction();
        try {
            parent::baseAdd($data);
            MemberPointRepository::getInstance()->initAdd($data['member_id'], $data['point']);
            PointLogRepository::getInstance()->baseAdd([
                'member_id'=>$data['member_id'],
                'type'=>PointCode::INCOME_POINT,
                'point_type'=>PointCode::TYPE_SIGN,
                'point'=>$data['point'],
                'old_total'=>$re['point'] ?? 0,
                'new_total'=>($re['point'] ?? 0) + $data['point'],
                'status'=>PointCode::STATUS_YES,
                'remark'=>PointCode::$type_msg[PointCode::TYPE_SIGN],
            ]);
            MessageRepository::getInstance()->point($data['member_id'],Message::MS_POINT_ADD, PointCode::TYPE_SIGN, $data['point']);
            DB::commit();
        }catch (Exception $e){
            DB::rollBack();
            Log::getInstance()->error('事务失败', [$e->getMessage()]);
        }

        return true;
    }

    /**
     * Notes: 获取会员签到记录
     * Author: Bruce.z
     * DateTime: 2022/7/18 17:39
     * @param int $member_id
     * @param string $date
     * @return array
     */
    public function initData(int $member_id, string $date):array
    {
        $date = $date . '-01 00:00:00';
        $time_start = Carbon::parse($date)->startOfMonth()->timestamp;
        $time_end =  Carbon::parse($date)->endOfMonth()->timestamp;

        $res = $this->getRows([
            'member_id'=>$member_id,
            ['create_time','>', $time_start],
            ['create_time','<', $time_end],
        ], ['id','create_time']);

        $return = [];
        array_walk($res, function ($re) use (&$return){
            $return[] = date('Y-m-d', $re['create_time']);
        });

        return $return;
    }
}
